package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.framework.aspectj.lang.annotation.SignatureValidation;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/mobileUser/type")
public class UserTypeController {

    @Autowired
    private ISysTypeService sysTypeService;
    @Autowired
    private ISysTypeDetailService sysTypeDetailService;
    @Autowired
    private ISysSkuService sysSkuService;
    @Autowired
    private ISysTypeCommentService sysTypeCommentService;
    @Autowired
    private ISysOrderEvaluateService orderEvaluateService;
    @Autowired
    private ISysServiceOrderService serviceOrderService;
    @Autowired
    private ISysMobileUserService sysMobileUserService;

    @SignatureValidation
    @PostMapping("queryChildrenType")
    public AjaxResult queryChildrenType(@RequestBody SysType sysType) {
        return AjaxResult.success(sysTypeService.queryChirdrenName(sysType));
    }

    @ApiOperation("查询服务的详情")
    @SignatureValidation
    @PostMapping("getChildrenType")
    public AjaxResult getChildrenType(@RequestBody SysType sysType) {
        List<SysType> typeList = sysTypeService.queryChirdrenName(sysType);
        for (int i = 0; i < typeList.size(); i++) {
            Long typeDetailId = typeList.get(i).getTypeDetailId();
            SysTypeDetail sysTypeDetail = sysTypeDetailService.selectSysTypeDetailById(typeDetailId);
            typeList.get(i).setSysTypeDetail(sysTypeDetail);
        }
        return AjaxResult.success(typeList);
    }

    @ApiOperation("查询热门工种")
    @GetMapping("/getTypeList")
    public AjaxResult getTypeList() {
        SysType sysType = new SysType();
        List<SysType> list = sysTypeService.selectSysTypeList(sysType);
        List<SysType> typeList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            try {
                String ancestors = list.get(i).getAncestors();
                String[] split = ancestors.split(",");
                int length = split.length;
                if (list.get(i).getParentTypeId() != null && length == 1) {
                    typeList.add(list.get(i));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return AjaxResult.success(typeList);
    }

    @ApiOperation("查询详情")
    @GetMapping("/getDetail/{typeId}")
    public AjaxResult getDetail(@PathVariable("typeId") Long typeId) {
        SysType sysType = sysTypeService.selectSysTypeById(typeId);
        Long typeDetailId = sysType.getTypeDetailId();
        SysTypeDetail sysTypeDetail = sysTypeDetailService.selectSysTypeDetailById(typeDetailId);
        SysSku sysSku = new SysSku();
        sysSku.setTypeDetailId(typeDetailId);
        List<SysSku> sysSkus = sysSkuService.selectSysSkuList(sysSku);
        SysSkuSku sysSkuSku = new SysSkuSku();
        sysSkuSku.setSysSku(sysSkus);
        sysSkuSku.setSysTypeDetail(sysTypeDetail);
        sysType.setSysSkuSku(sysSkuSku);
        SysOrderEvaluate sysOrderEvaluate = new SysOrderEvaluate();
        //评论
        List<SysOrderEvaluate> evaluateList = new ArrayList<>();
        List<SysOrderEvaluate> sysOrderEvaluateList = orderEvaluateService.selectSysOrderEvaluateList(sysOrderEvaluate);
        for (int i = 0; i < sysOrderEvaluateList.size(); i++) {
            String orderId = sysOrderEvaluateList.get(i).getOrderId();
            SysServiceOrder sysServiceOrder = serviceOrderService.selectSysServiceOrderById(Long.valueOf(orderId));
            Long detailId = sysServiceOrder.getTypeDetailId();
            if (typeDetailId.equals(detailId)) {
                SysMobileUser sysMobileUser = sysMobileUserService.selectSysMobileUserById(sysOrderEvaluateList.get(i).getMobileUserId());
                sysOrderEvaluateList.get(i).setSysMobileUser(sysMobileUser);
                evaluateList.add(sysOrderEvaluateList.get(i));
            }
        }

        Double values = 0.0;
        Double size = Double.valueOf(evaluateList.size());
        for (int i = 0; i < evaluateList.size(); i++) {
            Double value = evaluateList.get(i).getValue();
            values = values + value;
        }
        double v = (values / (size * 5.0)) * 100;
        sysType.setHighPraiseRate(v);
        sysType.setSysOrderEvaluateList(evaluateList);
        return AjaxResult.success(sysType);
    }


}
